<!DOCTYPE html>
<html>
<head>
<script src="../../../resources/js-test.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>
<script>
description('Tests for .valueAsNumber with &lt;input type=month>.');

var input = document.createElement('input');
input.type = 'month';

function valueAsNumberFor(stringValue) {
    input.value = stringValue;
    return input.valueAsNumber;
}

function setValueAsNumberAndGetValue(y, m) {
    input.valueAsNumber = (y - 1970) * 12 + m - 1;
    return input.value;
}

shouldBe('valueAsNumberFor("")', 'Number.NaN');
shouldBe('valueAsNumberFor("1969-01")', '-12');
shouldBe('valueAsNumberFor("1969-12")', '-1');
shouldBe('valueAsNumberFor("1970-01")', '0');
shouldBe('valueAsNumberFor("1970-12")', '11');
shouldBe('valueAsNumberFor("1971-01")', '12');
shouldBe('valueAsNumberFor("2009-12")', '(2009-1970)*12+11');

shouldBe('input.valueAsNumber = -1; input.value', '"1969-12"');
shouldBe('input.valueAsNumber = 0; input.value', '"1970-01"');
shouldBe('setValueAsNumberAndGetValue(2009, 12)', '"2009-12"');
shouldBe('setValueAsNumberAndGetValue(10000, 1)', '"10000-01"');

shouldBe('setValueAsNumberAndGetValue(-1, 1)', '""');
shouldBe('setValueAsNumberAndGetValue(0, 12)', '""');
shouldBe('setValueAsNumberAndGetValue(1, 1)', '"0001-01"');
shouldBe('setValueAsNumberAndGetValue(1, 2)', '"0001-02"');
shouldBe('setValueAsNumberAndGetValue(275760, 9)', '"275760-09"');
shouldBe('setValueAsNumberAndGetValue(275760, 10)', '""');

// Date.UTC() of V8 throws an exception for the following value though JavaScriptCore doesn't.
// shouldBe('setValueAsNumberAndGetValue(275760, 8, 14)', '"275760-09"');

debug('Tests to set invalid values to valueAsNumber:');
shouldBe('input.value = ""; input.valueAsNumber = null; input.value', '"1970-01"');
shouldBeNaN('input.valueAsNumber = "foo"; input.valueAsNumber');
shouldBeEqualToString('input.valueAsNumber = "foo"; input.value', '');
shouldBeNaN('input.valueAsNumber = NaN; input.valueAsNumber');
shouldBeEqualToString('input.valueAsNumber = NaN; input.value', '');
shouldBeNaN('input.valueAsNumber = Number.NaN; input.valueAsNumber');
shouldBeEqualToString('input.valueAsNumber = Number.NaN; input.value', '');
shouldThrow('input.valueAsNumber = Infinity', '"TypeError: Failed to set the \'valueAsNumber\' property on \'HTMLInputElement\': The value provided is infinite."');
shouldThrow('input.valueAsNumber = Number.POSITIVE_INFINITY', '"TypeError: Failed to set the \'valueAsNumber\' property on \'HTMLInputElement\': The value provided is infinite."');
shouldThrow('input.valueAsNumber = Number.NEGATIVE_INFINITY', '"TypeError: Failed to set the \'valueAsNumber\' property on \'HTMLInputElement\': The value provided is infinite."');
</script>
</body>
</html>
